package logic

import (
	"context"
	"edu-pro/models/mysql"
	"errors"

	"edu-pro/edurpc/edurpc"
	"edu-pro/edurpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CancelCollectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCancelCollectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CancelCollectLogic {
	return &CancelCollectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CancelCollectLogic) CancelCollect(in *edurpc.CancelCollectRequest) (*edurpc.CancelCollectResponse, error) {
	// 取消收藏
	var collect mysql.Collection
	mysql.DB.Debug().Where("user_id = ? AND course_id = ?", in.UserId, in.CourseId).Find(&collect)
	if collect.ID == 0 {
		return nil, errors.New("未加入收藏")
	}
	mysql.DB.Debug().Where("user_id = ? AND course_id = ?", in.UserId, in.CourseId).Delete(&collect)
	return &edurpc.CancelCollectResponse{}, nil
}
